//
//  ModifyPersonalInfoViewController.m
//  YunGou
//
//  Created by macbook on 16/5/18.
//  Copyright © 2016年 macbook. All rights reserved.
//

#import "ModifyPersonalInfoViewController.h"
#import "UIBarButtonItem+Extension.h"
#import "NSString+Regexp.h"
#import "PersonalInfoViewController.h"

@interface ModifyPersonalInfoViewController ()
@property (weak, nonatomic) IBOutlet UITextField *contentText;

@end

@implementation ModifyPersonalInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *rightBarItem = [UIBarButtonItem barButtonTitle:@"保存" image:nil frame:CGRectMake(0, 0, 44, 44) target:self sel:@selector(saveClicked)];
    self.navigationItem.rightBarButtonItem = rightBarItem;
    if (_type == UserName) {
        _contentText.text = [UserModel sharedInstance].username;
    }else if (_type == QQ){
        _contentText.text = [UserModel sharedInstance].qq;
    }else if (_type == Email){
        _contentText.text = [UserModel sharedInstance].email;
    }
}

- (void)saveClicked{
    NSString *contentText = _contentText.text;
    if (contentText.length == 0) {
        [MBProgressHUD showSuccess:@"内容不能为空"];
    }else{
        if (_type == UserName) {//更新用户名
            [NetWorkTool updateUserInfoWithNickName:contentText img:nil gender:nil birth:nil qq:nil email:nil address:nil success:^(id object) {
                if ([object[@"code"] integerValue] == 1) {
                    [MBProgressHUD showSuccess:@"修改成功"];
                    [[UserModel sharedInstance] setValue:_contentText.text forKey:@"username"];
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [self.navigationController popViewControllerAnimated:YES];
                    });
                }
            } failure:^(NSError *error) {
                FZLog(@"%@",error);
            }];
        }else if (_type == QQ){//更新QQ
            [NetWorkTool updateUserInfoWithNickName:nil img:nil gender:nil birth:nil qq:contentText email:nil address:nil success:^(id object) {
                if ([object[@"code"] integerValue] == 1) {
                    [MBProgressHUD showSuccess:@"修改成功"];
                    [[UserModel sharedInstance] setValue:_contentText.text forKey:@"qq"];
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [self.navigationController popViewControllerAnimated:YES];
                    });
                }
            } failure:^(NSError *error) {
                
            }];
        }else{//更新邮箱
            if ([NSString isEmailNumber:contentText]) {
                [NetWorkTool updateUserInfoWithNickName:nil img:nil gender:nil birth:nil qq:nil email:contentText address:nil success:^(id object) {
                    if ([object[@"code"] integerValue] == 1) {
                        [MBProgressHUD showSuccess:@"修改成功"];
                        [[UserModel sharedInstance] setValue:_contentText.text forKey:@"email"];
                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                            [self.navigationController popViewControllerAnimated:YES];
                        });
                    }
                } failure:^(NSError *error) {
                    
                }];
            }else{
                [MBProgressHUD showError:@"邮箱格式不正确"];
            }
        }
    }
}

- (IBAction)clearClicked:(id)sender {
    _contentText.text = @"";
}

- (void)viewWillDisappear:(BOOL)animated{
    PersonalInfoViewController *personalInfoVC = SB(@"Setting", @"PersonalInfoVC");
    [personalInfoVC.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
